import { getUserInfo, getUserIdToken } from '@/api/api-user';
import { useLoginStore, useUserInfoStore } from '@/stores/user';
import type { UserInfoT } from '@/shared/@types/type-user';
import Cookies from 'js-cookie';
import { handleError } from '@/shared/utils';

const LOGIN_URL = import.meta.env.VITE_LOGIN_URL;
const DOMAIN = import.meta.env.VITE_COOKIE_DOMAIN;
// 登录状态
// -1: 登录失败；0：未登录；1：登录中；2：登录成功
export enum LOGIN_STATUS {
  FAILED = -1,
  NOT = 0,
  DOING = 1,
  DONE = 2,
}

export type LoginStatusT =
  | typeof LOGIN_STATUS.FAILED
  | LOGIN_STATUS.NOT
  | LOGIN_STATUS.DOING
  | LOGIN_STATUS.DONE;

// 登录存储字段
export const LOGIN_KEYS = {
  CSRF_TOKEN: '_U_T_',
};

// 修改pinia登录状态
const setStatus = (status: LoginStatusT) => {
  const loginStore = useLoginStore();
  loginStore.setLoginStatus(status);
};

// 获取用户认证凭据
export function getUserAuth() {
  return {
    csrfToken: Cookies.get(LOGIN_KEYS.CSRF_TOKEN),
  };
}

// 清除用户认证凭据
export function clearUserAuth() {
  // 清除内存中用户信息
  const userInfoStore = useUserInfoStore();
  userInfoStore.$reset();
  // 清除cookie
  Cookies.remove(LOGIN_KEYS.CSRF_TOKEN, { path: '/', domain: DOMAIN });
}

// 登录之后的回调
const afterLogined = (userInfo: UserInfoT) => {
  const { username } = userInfo;
  if (!username) {
    setStatus(LOGIN_STATUS.FAILED);
    clearUserAuth();
  }
  setStatus(LOGIN_STATUS.DONE);

  const userInfoStore = useUserInfoStore();
  userInfoStore.$patch(userInfo);
};

// 退出
export async function doLogout() {
  try {
    const idTokenRes = await getUserIdToken();
    if (idTokenRes.code === 200) {
      setStatus(LOGIN_STATUS.NOT);
      clearUserAuth();
      window.location.href = location.href;
    } else {
      handleError();
    }
  } catch (error) {
    /* empty */
  }
}

// 获取用户信息
export async function requestUserInfo() {
  const { csrfToken } = getUserAuth();
  if (csrfToken) {
    try {
      setStatus(LOGIN_STATUS.DOING);
      const res = await getUserInfo();
      if (res && res.data) {
        afterLogined(res.data);
      } else {
        doLogout();
        setStatus(LOGIN_STATUS.FAILED);
      }
    } catch (err) {
      doLogout();
      setStatus(LOGIN_STATUS.FAILED);
    }
  }
}

// authing认证登录
export async function doLogin() {
  const { lang } = getLanguage();
  try {
    window.location.href = `${LOGIN_URL}/login?redirect_uri=${decodeURIComponent(
      window.location.href
    )}&lang=${lang}`;
  } catch (error) {
    setStatus(LOGIN_STATUS.FAILED);
  }
}
export function getLanguage() {
  if (location.pathname.includes('/zh/')) {
    return {
      lang: 'zh',
      language: 'zh-CN',
    };
  }
  return {
    lang: 'en',
    language: 'en-US',
  };
}
